﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class AdminBaseController:Controller
    {
        protected adminEntity curmanage=new adminEntity();
        protected List<string> qxlist=new List<string>();
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.ActionDescriptor is ActionDescriptor)
            {
                var actionDescriptor = filterContext.ActionDescriptor as ActionDescriptor;
                var isDefined = actionDescriptor.GetCustomAttributes(true).Any(a => a is AllowAnonymousAttribute);
                if (isDefined)
                {
                    base.OnActionExecuting(filterContext);
                    return;
                }
            }
            if (Session == null || Session["admin"] == null)
            {
                base.OnActionExecuting(filterContext);
                filterContext.Result = MyJson(new JsonMsg { code=401,msg="登录超时！"});
            }
            else
            {
                curmanage = Session["admin"] as adminEntity;
                var admintype=new admintypeCore().GetOne(curmanage.utype);
                if (Session["qxlist"] == null)
                {
                    Session["qxlist"] = admintype.purviews==null?new List<string>():admintype.purviews.Split(',').ToList();
                }
                qxlist = Session["qxlist"] as List<string>;
                base.OnActionExecuting(filterContext);
            }
        }

        protected bool HasQx(string qx)
        {
            return qxlist.Contains(qx)||qxlist.Contains("allowall");
        }
        protected ActionResult Success()
        {
            return MyJson(new JsonMsg { code = 200, msg = new { } });
        }
        protected ActionResult Success(object msg)
        {
            return MyJson(new JsonMsg { code = 200, msg = msg });
        }
        protected ActionResult Error()
        {
            return MyJson(new JsonMsg { code = 403, msg = new { } });
        }
        protected ActionResult Error(object obj)
        {
            return MyJson(new JsonMsg {code = 403, msg = obj});
        }
        protected ActionResult MyJson(object obj)
        {
            return new CustomJsonResult { Data = obj };
        }

        protected void Log(string msg)
        {
            //var model=new logEntity();
            //model.addtime=DateTime.Now;
            //model.ip = Utils.GetAddressIP();
            //model.mid = ((adminEntity) Session["admin"]).ID;
            //model.title = msg;
            //new logCore().Save(model);
        }

    }
}

